import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.*;
import static java.lang.System.out;

/**
 * Created by sunshengchao on 17/1/5.
 */

public class Test {
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/information?useUnicode=true&characterEncoding=utf-8&useSSL=false";

    static final String USER = "root";
    static final String PASS = "Sunsun123,";
    public static void main(String[] args){
        Connection conn = null;
        Statement stmt = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
            stmt = conn.createStatement();

            int[] tid = {24, 25, 47, 27, 33, 32, 153, 51, 152, 28, 31, 30, 59, 29, 54, 130, 20, 154, 156, 137, 131, 166, 71, 22, 26, 126, 127,138,21,76,161,162,163,39};
            for(int i=0;i<tid.length;i++){
                for(int m=0;m<2000;m++) {
                CloseableHttpClient httpClient = HttpClients.createDefault();
                HttpGet req = new HttpGet("http://api.bilibili.com/archive_rank/getarchiverankbypartion?callback=?&type=jsonp&tid="+tid[i]+"&pn="+m);
                req.addHeader("Accept", "application/json,text/javascript,*/*;q=0.01");
                req.addHeader("Accept-Encoding", "gzip,deflate,sdch");
                req.addHeader("Accept=Language", "zh-CN,zh;q=0.8");
                req.addHeader("Content-Type", "text/html;charset=UTF-8");
                req.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586");
                HttpResponse rep = httpClient.execute(req);
                HttpEntity repEntity = rep.getEntity();
                String content = EntityUtils.toString(repEntity);
                    content=Convert(content);
                    content=content.replace("\'","\\'");

                for(int n=0;n<20;n++){
                    int aidc  = content.indexOf("aid");
                    int aidd  = content.indexOf("tid");
                    String aid=content.substring(aidc+5,aidd-2);
                    out.println(aid);
                    int titlec  = content.indexOf("title");
                    int titled  = content.indexOf("attribute");
                    String title=content.substring(titlec+8,titled-3);
                    out.println(title);
                    int authorc  = content.indexOf("author");
                    int authord  = content.indexOf("face\"");
                    String author=content.substring(authorc+9,authord-3);
                    out.println(author);
                    int favoritec  = content.indexOf("favorite");
                    int favorited  = content.indexOf("coin");
                    int favorite=Integer.parseInt(content.substring(favoritec+10,favorited-2));
                    out.println(favorite);
                    int coinc  = content.indexOf("coin");
                    int coind  = content.indexOf("share");
                    int coin=Integer.parseInt(content.substring(coinc+6,coind-2));
                    out.println(coin);
                    String sql="insert into information_tbl VALUES('" + aid + "','" + title + "','" + author + "','" + favorite + "','" + coin + "')";
                    stmt.executeUpdate(sql);
                    content=content.substring(authord+10);}
                }
            }
        } catch(Exception e) {System.out.println(e.getMessage());}

        try{
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
            stmt = conn.createStatement();
            String sql;
            sql="select * from information_tbl order by favorite desc limit 4";
            ResultSet rs=stmt.executeQuery(sql);
            while(rs.next()){
                String av=rs.getString("av");
                String title=rs.getString("title");
                String Url1="http://www.ibilibili.com/video/av"+av;
                String Pathname=title;
                httpDownload(Url1,title);
                String s2=new String(Pathname.getBytes("ISO-8859-1"),"UTF-8");
                int indexStart= s2.indexOf("<a download=\"download\" href =\"http://www.bilibilijj.com/Files/Download");
                int indexEnd= s2.indexOf("www.bilibilijj.com.mp4?mp3=true");
                Pathname=title+".mp4";
                String Url=s2.substring(indexStart,indexEnd);
                httpDownload(Url,Pathname);
            }
        } catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    public static void httpDownload(String httpUrl,String saveFile){
        // 下载网络文件
        int bytesum = 0;
        int byteread = 0;
        URL url = null;
        try {
            url = new URL(httpUrl);
        } catch (MalformedURLException e1) {
            System.out.println(e1.getMessage());
        }
        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            FileOutputStream fs = new FileOutputStream(saveFile);

            byte[] buffer = new byte[1024];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                System.out.println(bytesum);
                fs.write(buffer, 0, byteread);
            }
            fs.close();
            inStream.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        };
    }

    public static String Convert(String utfString) {
        StringBuilder sb = new StringBuilder();
        int i = -1;
        int pos = 0;

        while ((i = utfString.indexOf("\\u", pos)) != -1) {
            sb.append(utfString.substring(pos, i));
            if (i + 5 < utfString.length()) {
                pos = i + 6;
                sb.append((char) Integer.parseInt(utfString.substring(i + 2, i + 6), 16));
            }
        }

        return sb.toString();
    }
}
